package com.qunar.wan.service;

import com.google.common.base.Preconditions;
import com.qunar.wan.common_api.model.EmailTask;
import com.qunar.wan.common_api.model.Query;
import com.qunar.wan.common_api.model.enums.EmailType;
import com.qunar.wan.common_api.service.EmailService;
import com.qunar.wan.common_api.service.mapper.EmailTaskMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: devry Date: 12/18/13 Time: 11:55 AM
 */
@Service("emailService")
public class EmailServiceImpl extends BaseServiceImpl<EmailTask, EmailTaskMapper> implements EmailService {
    /**
     * 批量插入邮件发送任务
     * 
     * @param emailType
     * @param actId
     * @param userEmails
     */
    @Override
    public void send(EmailType emailType, int actId, List<String> userEmails) {
        Preconditions.checkArgument(actId > 0, "actId must more than 0");
        if (userEmails.size() > 0) {
            for (String userEmail : userEmails) {
                send(emailType, actId, userEmail);
            }
        }
    }

    /**
     * 插入邮件发送任务
     * 
     * @param emailType
     * @param actId
     * @param userEmail
     */
    @Override
    public void send(EmailType emailType, int actId, String userEmail) {
        Preconditions.checkNotNull(userEmail);
        // 拼装一个EmailTask
        EmailTask emailTask = new EmailTask();
        emailTask.setActId(actId);
        emailTask.setUserEmail(userEmail);
        emailTask.setEmailType(emailType);
        insertObj(emailTask);
    }

    /**
     * 查找所有的邮件发送任务
     * 
     * @return
     */
    @Override
    public List<EmailTask> selectEmailTask() {
        return selectByQuery(new Query());

    }

    /**
     * 删除已发送的邮件
     * 
     * @param taskId
     */
    @Override
    public void deleteEmailTask(int taskId) {
        deleteByPK(taskId);
    }

}
